﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace HAOest.Forms.Buttons
{
    #region//控件状态
    /// <summary>
    /// 控件状态
    /// </summary>
    public enum State
    {
        /// <summary>
        /// 无
        /// </summary>
        Normal = 0,
        /// <summary>
        /// 获得焦点
        /// </summary>
        Focused = 1,
        /// <summary>
        /// 失去焦点
        /// </summary>
        LostFocused = 2,
        /// <summary>
        /// 鼠标指针进入控件
        /// </summary>
        MouseEnter = 3
    }
    #endregion

    public class RichButton : Control
    {
        #region//私有变量
        private int bmp_Left;
        private const int bmp_Top = 8;
        private const int bmp_Size = 48;
        private bool clicked = false;
        private State state = State.Normal;

        private Bitmap icon = Properties.Resources.AdvanceTools_MenuMgr;
        private string text = null;
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public RichButton()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.OptimizedDoubleBuffer, true);               //双缓冲防止重绘时闪烁
            SetStyle(ControlStyles.UserPaint, true);                      //自定义绘制控件内容
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);   //模拟透明
            SetStyle(ControlStyles.Selectable, true);                     //接收焦点
            SetStyle(ControlStyles.ResizeRedraw, true);
            Size = new Size(72, 80);                                      //初始大小
            Font = new Font("微软雅黑", 9);                                //控件字体
            BackColor = Color.Transparent;
        }
        #endregion

        #region 属性
        /// <summary>
        /// 获取或设置控件显示的图片
        /// </summary>
        [Description("获取或设置控件显示的图标")]
        public Bitmap Icon
        {
            get { return icon; }
            set
            {
                icon = value;
                Invalidate(false);
            }
        }

        /// <summary>
        /// 重写控件焦点属性
        /// </summary>
        [Description("重写控件焦点属性")]
        public bool Clicked
        {
            get { return clicked; }
            set
            {
                clicked = value;

                if (clicked)
                    state = State.Focused;
                else
                    state = State.LostFocused;

                Invalidate(false);
            }
        }

        /// <summary>
        /// 与控件关联的文本
        /// </summary>
        [Description("与控件关联的文本。"), DefaultValue(null)]
        public new string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
                Invalidate(false);
            }
        }
        #endregion

        #region 重载事件
        /// <summary>
        /// 自定义绘制
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            switch (state)
            {
                case State.Focused:
                    {
                        DrawSelected(g);
                        break;
                    }
                case State.MouseEnter:
                    {
                        if (!Clicked)
                            g.DrawImage(Properties.Resources.rich_button_layer_opacity_20, ClientRectangle);
                        else
                            DrawSelected(g);
                        break;
                    }
            }

            DrawIcon(g);
            DrawText(g);
        }
/*
        /// <summary>
        /// 焦点进入
        /// </summary>
        /// <param name="e"></param>
        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            Clicked = true;
        }

        /// <summary>
        /// 失去焦点
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            Clicked = false;
        }
 */

        /// <summary>
        /// 禁止调整大小
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            Width = 72;
            Height = 80;
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            state = State.MouseEnter;
            Invalidate(false);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (!Clicked)
            {
                state = State.LostFocused;
                Invalidate(false);
            }
        }

        /// <summary>
        /// 只响应单击鼠标左键事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e)
        {
            MouseEventArgs m = (MouseEventArgs)e;
            if (m.Button == MouseButtons.Left)
            {
                base.OnClick(e);
                Focus();
            }
        }
        #endregion

        #region 方法
        void DrawSelected(Graphics g)
        {
            g.DrawImage(Properties.Resources.rich_button_layer_opacity_50, ClientRectangle);
        }

        void DrawIcon(Graphics g)
        {
            if (icon != null)
            {
                Bitmap bmp = ScaleZoom(icon);
                bmp_Left = (Width - bmp.Width) / 2;
                g.DrawImage(bmp, new Rectangle(bmp_Left, bmp_Top, bmp.Width, bmp.Height));
            }
        }

        void DrawText(Graphics g)
        {
            SizeF size = g.MeasureString(text, Font);
            g.DrawString(text, Font, new SolidBrush(ForeColor), (Width - size.Width) / 2, 58);
        }

        /// <summary>
        /// 按比例缩放图片
        /// </summary>
        /// <param name="bmp">原图片</param>
        /// <returns></returns>
        public Bitmap ScaleZoom(Bitmap bmp)
        {
            if (bmp != null)
            {
                double zoomScale;
                if (bmp.Width > bmp_Size || bmp.Height > bmp_Size)
                {
                    double imageScale = (double)bmp.Width / (double)bmp.Height;
                    double thisScale = 1;

                    if (imageScale > thisScale)
                    {
                        zoomScale = (double)bmp_Size / (double)bmp.Width;
                        return BitMapZoom(bmp, bmp_Size, (int)(bmp.Height * zoomScale));
                    }
                    else
                    {
                        zoomScale = (double)bmp_Size / (double)bmp.Height;
                        return BitMapZoom(bmp, (int)(bmp.Width * zoomScale), bmp_Size);
                    }
                }
            }
            return bmp;
        }

        /// <summary>
        /// 图片缩放
        /// </summary>
        /// <param name="bmpSource">源图片</param>
        /// <param name="bmpHeight">缩放图片的宽度</param>
        /// <param name="bmpWidth">缩放图片的高度</param>
        /// <returns>缩放的图片</returns>
        public Bitmap BitMapZoom(Bitmap bmpSource, int bmpWidth, int bmpHeight)
        {
            Bitmap bmp, zoomBmp;
            try
            {
                bmp = new Bitmap(bmpSource);
                zoomBmp = new Bitmap(bmpWidth, bmpHeight);
                Graphics gh = Graphics.FromImage(zoomBmp);
                gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gh.DrawImage(bmp, new Rectangle(0, 0, bmpWidth, bmpHeight), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);

                gh.Dispose();
                return zoomBmp;
            }
            catch
            { }
            finally
            {
                bmp = null;
                zoomBmp = null;
                GC.Collect();
            }
            return null;
        }
        #endregion
    }
}
